﻿/// <reference path="tfcore.js" />
/// <reference path="ednlog.js" />
/// <reference path="jquery-1.2.6-vsdoc.js" />
// #pragma NoCompStart
/*
tfpopup.js  1.0.8
handles modal dialog boxes, etc
*/
// #pragma NoCompEnd

// ModalDlg code
var alertTypes = { Error: 1, Warning: 2, Information: 3 };
var locTypes = { NW: 1, N: 2, NE: 3, E: 4, SE: 5, S: 6, SW: 7, W: 8, CEN: 9 };
var contentTypes = { iframe: 1, html: 2, alert: 3, MsgBox: 4, csMsgBox: 5 };
var msgboxType = { ok: 1, yes: 2, yesno: 3, yesnocancel: 4 };
var returnFunc = { pbContentPage: 1, pbPrevDlg: 2 };  // cause postback of IFrame, or Postback of Previous Modal Dialog, OR send a javascript function to run after button clicked.
var gTabbableTags = [];
var gTabIndexes = [];
var gIteration = -1;
var gDefaultPage = "about:blank";
var iTimeoutID;
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = ["A","BUTTON","TEXTAREA","INPUT","IFRAME"];

// If using Mozilla or Firefox, use Tab-key trap.
//if (!document.all){document.onkeypress = keyPressHandler;}
function disableTabIndexes() {if (document.all) {var i = 0;	for (var j = 0; j < gTabbableTags.length; j++) {var tagElements = document.getElementsByTagName(gTabbableTags[j]);for (var k = 0 ; k < tagElements.length; k++) {gTabIndexes[i] = tagElements[k].tabIndex;tagElements[k].tabIndex="-1";	i++;}}}}
function restoreTabIndexes() {if (document.all) {var i = 0;	for (var j = 0; j < gTabbableTags.length; j++) {var tagElements = document.getElementsByTagName(gTabbableTags[j]);for (var k = 0 ; k < tagElements.length; k++) {tagElements[k].tabIndex = gTabIndexes[i];tagElements[k].tabEnabled = true;i++;}}}}
function getWidth(width){var pWidth; if (width == undefined) {width = '85%';} if(isNaN(width)){if (width.search(/\%/) != -1) {pWidth = getViewPortWidth() * (width.replace("\%","")/100);}} else{pWidth = width;}return pWidth;}
function getHeight(height){var pHeight;if (height == undefined) {height = '90%';}if(isNaN(height)){if (height.search(/\%/) != -1) {pHeight = getViewPortHeight() * (height.replace("\%","")/100);}} else{pHeight = height; }return pHeight;}
function getScrollTop() { if (top.pageYOffset){ return top.pageYOffset;} else if (top.document.documentElement && top.document.documentElement.scrollTop) { return top.document.documentElement.scrollTop;} else if (top.document.body) {return top.document.body.scrollTop;}}

function ModalDlg(options) {
    /// <summary>Instantiates a new ModalDlg as either an alert or an iframe</summary>
    /// <param name="width">width of the dialog</param>
    /// <param name="height">width of the dialog</param>
    /// <param name="loc">use the locTypes to see what is available (locTypes.N = top of screen)</param>
    /// <returns>nothing</returns>
    
    //Set up default options:
    var defaultOptions = {
        id: '',
        type: 2, //contentTypes.html
        width: 600,
        height: 600,
        loc: 'CEN',
        title: 'Dialog',
        alertText: g_jsErrorTxt,
        alertType: 0,
        alertTask: 'unknown',
        msgboxType: 0,
        msgboxQuestion: '',
        URL: '',
        HTML: '',
        returnFocusID: '',
        returnFunc: '',
        showClose: false,
        autoClose: -1,
        modal: false
    };
        options = options || {};
        for (var name in defaultOptions) {
            if (!options.hasOwnProperty(name)) {
                options[name] = defaultOptions[name];
            }
        }
        
    var _me = this; // Instantiated Object = "_me"
    gIteration++;
    _me.iter = gIteration;
    _me.id = options.id;
    _me.contentType = options.type;
    _me.width = options.width;
    _me.msgboxQuestion = options.msgboxQuestion;
    _me.msgboxType = options.msgboxType;
    _me.height = options.height;
    _me.loc = options.loc;
    _me.title = options.title;
    _me.blnClose = options.showClose;
    _me.alertText = options.alertText;
    _me.alertType = options.alertType;
    _me.autoClose = options.autoClose;
    _me.setFocusId = options.returnFocusID;
    _me.callBackFunc = options.returnFunc;
    _me.myHTML = options.HTML;
    _me.myURL = options.URL;
    _me.sTask = options.alertTask;
    _me.isModal = options.modal;
    
    var strType = "mdl";
    if (_me.contentType != contentTypes.iframe) {
        strType ="alt";  
    }

    _me.tBody = document.getElementById(strType + 'Dialog' + _me.iter);
    if (_me.tBody == null) {
        _me.tBody = document.createElement("div"); 
        _me.tBody.id = strType + "Dialog" + _me.iter;
        document.body.appendChild(_me.tBody); 
    }
    _me.Mask = document.createElement("div");
    _me.Mask.id = strType + "DlgMask_" + _me.iter;
    _me.Mask.className = "ModalDlgMask";
    _me.Con = document.createElement("div");
    _me.Con.id = strType + "DlgContainer_" + _me.iter;
    _me.Con.className = "ModalDlgContainer";
    
    _me.MDI = document.createElement("div");
    _me.MDTB = document.createElement("div");
    _me.MDT = document.createElement("div");
    _me.MDC = document.createElement("div");
    _me.HFLD = document.createElement("INPUT");
    _me.Content = null;
    _me.arrTemp = [];
        
            
    // private functions
    _me.setTask = function(sTask) { _me.sTask = sTask; };
    _me.setType = function(contentType) { _me.contentType = contentType; };
    _me.setURL = function(sUrl){_me.myURL=sUrl;};
    _me.setCallBackFunc = function(callBackFunc){_me.callBackFunc = callBackFunc;};
    _me.setFocusOnReturn = function(elId){_me.setFocusId = elId;};
    _me.setHTML = function(newHTML){_me.myHTML = newHTML;};
    _me.setModal = function(sModal){_me.isModal = sModal;};
    _me.setAlertType = function(aType) { _me.alertType = aType; };
    _me.setTitle = function(sTitle) { _me.title = sTitle; };
    _me.setLocation = function(locType) { _me.loc = locType; };
    _me.setAutoClose = function(sSeconds) { _me.autoClose = sSeconds * 1000; };

    _me.init = function() {
        var arrTemp = [];
        _me.MDI.id = "MDI" + _me.iter;
        _me.MDTB.id = "MDTB" + _me.iter;
        _me.MDT.id = "MDT" + _me.iter;
        _me.MDC.id = "MDI" + _me.iter;
        _me.HFLD.id = "HFLD" + _me.iter;
        _me.HFLD.setAttribute("type", "button");

        var cImg = document.createElement("img");
        cImg.id = "popCloseBox_" + _me.iter;

        switch (_me.contentType) {
            case contentTypes.MsgBox: case contentTypes.csMsgBox:
                var image = edIs;
                var cs = _me.contentType == contentTypes.csMsgBox ? true : false;
                var onClose = "javascript:edRaiseEvent('" + _me.id + "','OnClose','~t');";
                var strButtons = '';
                if (_me.id == '') { _me.id = 'msgBoxValue'; }

                if (_me.callBackFunc == returnFunc.pbContentPage) {
                    onClose += "top.frames['ifMainContent'].__doPostBack('" + _me.id + "','~t');";
                }
                else if (_me.callBackFunc == returnFunc.pbPrevDlg) {
                onClose += "javascript:edRaiseEvent('" + _me.id + "','OnClose','~t');postBackbPrevDlg('" + _me.id + "','~t');";
                }

                _me.returnFunc = ""; // clear out return function as we already raisevent!
                onClose += "g_MsgBox.hide(" + _me.contentType + ");";

                switch (_me.msgboxType) {
                    case msgboxType.ok:
                        image = edIs;

                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "ok") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td style='width:50%;'>&nbsp;</td>";
                        break;
                    case msgboxType.yes:
                        image = edIs;
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                    case msgboxType.yesno:
                        image = edIs;
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                        strButtons += "<td>&nbsp;&nbsp;&nbsp;</td>";
                        strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                    case msgboxType.yesnocancel:
                        image = edIs;
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                        strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                        strButtons += "<td><input id='s3' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "cancel") + "\" type=button value=Cancel name=MessageBoxIDCancel1 /></td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                }
                cImg.setAttribute("src", edXs);
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                var strHTML = "<table style='width: 100%;text-align:center;background-color: lightgrey;'>";
                strHTML += "<tr><td ><img style='border: black 1px solid; height:25px' src='" + image + "' alt='' /></td>";
                strHTML += "<td style='text-align:left;padding:5px;' colspan='4'>" + _me.msgboxQuestion + "</td></tr><tr>";
                strHTML += strButtons;
                strHTML += "</tr></table></div></div>";
                _me.Content.innerHTML = strHTML;
                _me.setModal(true);
                break;

            case contentTypes.html:
                var image = edIs;
                switch (_me.alertType) {
                    case alertTypes.Error:
                        _me.autoClose = -1;
                        top.edNLog.error(g_jsErrorTxt, _me.sTask);
                        image = edEs;
                        break;
                    case alertTypes.Warning:
                        top.edNLog.warning(g_jsErrorTxt, _me.sTask);
                        image = edWs;
                        break;
                    case alertTypes.Information:
                        top.edNLog.info(g_jsErrorTxt, _me.sTask);
                        image = edIs;
                        break;
                }
                cImg.setAttribute("src", edXs);
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                _me.Content.innerHTML = "<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.errText + "</td></tr></table></div></div>";
                break;
            case contentTypes.alert:
                var image = edIs;
                switch (_me.alertType) {
                    case alertTypes.Error:
                        _me.autoClose = -1;
                        top.edNLog.error(g_jsErrorTxt, _me.sTask);
                        image = edEs;
                        break;
                    case alertTypes.Warning:
                        top.edNLog.warning(g_jsErrorTxt, _me.sTask);
                        image = edWs;
                        break;
                    case alertTypes.Information:
                        top.edNLog.info(g_jsErrorTxt, _me.sTask);
                        image = edIs;
                        break;
                }
                cImg.setAttribute("src", edXs);
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                _me.Content.innerHTML = "<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.alertText + "</td></tr></table></div></div>";
                break;
            case contentTypes.iframe:
                _me.MDI.className = "ModalDlgInner";
                _me.MDTB.className = "ModalDlgTitleBar";
                _me.MDT.className = "ModalDlgTitle";
                _me.MDT.innerHTML = "<img src='" + gsfICON + "' alt=''></img>&nbsp;&nbsp;" + _me.title;
                _me.MDC.className = "ModalDlgControls";
                cImg.setAttribute("src", edX);
                _me.Content = document.createElement("iframe");
                _me.Content.setAttribute("src", _me.myURL);
                _me.Content.setAttribute("style", "width:100%;height:100%;background-color:transparent;");
                _me.Content.setAttribute("scrolling", "auto");
                _me.Content.setAttribute("frameborder", "0");
                _me.Content.setAttribute("allowtransparency", "true");
                _me.Content.className = "ModalDlgFrame";
                if (_me.id != '') {
                    _me.Content.id = _me.id;
                    _me.Content.setAttribute("name", _me.id);
                }
                else {
                    _me.Content.id = strType + "DlgFrame_" + _me.iter;
                    _me.Content.setAttribute("name", strType + "DlgFrame_" + _me.iter);
                }

                _me.Content.setAttribute("width", "100%");
                _me.Content.setAttribute("height", "100%");
                break;
            default: break;
        }

        _me.MDC.appendChild(cImg);
        _me.MDTB.appendChild(_me.MDT);
        _me.MDTB.appendChild(_me.MDC);
        _me.MDI.appendChild(_me.MDTB);
        _me.MDI.appendChild(_me.Content);
        _me.Con.appendChild(_me.MDI);
        _me.tBody.appendChild(_me.Mask);
        _me.tBody.appendChild(_me.Con);

        edEnableDrag(_me.Con.id);
        //shortcut.add("esc", _me.hide, {}, _me, true);
        //specific to GSF
        //shortcut.add("esc", function() { _me.hide(_me.contentType); }, { target: _me.tBody }, _me, true);
        edAddEvent(_me.MDC, "click", function() { _me.hide(_me.contentType); });
    };

    _me.show = function show() {
        var pWidth = getWidth(_me.width);
        var pHeight = getHeight(_me.height) - 30;

        _me.init();
        var t = getLocationPoint(pWidth, pHeight, _me.loc);
        _me.Con.style.left = t.x;
        _me.Con.style.top = t.y;
        if (_me.alertType > 2) { _me.autoClose = 4000; }

        if (_me.isModal) {
            //disableTabIndexes();
            _me.Mask.style.height = getViewPortHeight();
            _me.Mask.style.width = getViewPortWidth();
            _me.Mask.style.zIndex = 800 + (_me.iter * 3);
            _me.Mask.style.display = "block";
        }

        var titleBarHeight = parseInt(_me.MDTB.offsetHeight, 10);
        _me.Con.style.width = pWidth + "px";
        _me.Con.style.height = (pHeight + titleBarHeight) + "px";
        _me.Con.style.zIndex = 801 + (_me.iter * 3);
        _me.Con.style.display = "block";
        if (_me.autoClose > 0) { setTimeout(_me.hide, _me.autoClose); }

    };      // end show

    _me.destroy = function() {
        edRemoveEvent(_me.MDC, "click", function() { _me.hide(_me.contentType); });        
        edEnableDrag(_me.Con.id, '', '', '', false);
        removeChildSafe(_me.tBody); // Remove ALL children elements first to avoid memory leaks
        gIteration--;
        if (gIteration == -1) gIteration = 0;
    };

    _me.hide = function(iType) {
        if (_me!=null) {
            if (_me.contentType == iType) {
                _me.Con.style.display = 'none';
                var fn = "";
                if (_me.callBackFunc != null) { fn = _me.callBackFunc; }
                if (_me.setFocusId != null && _me.setFocusId.length > 0) { fn += ";setFocus(" + _me.setFocusId + ");"; }
                if (fn.length > 0) { setTimeout(fn, 10); }
                if (_me.isModal) {
                    //    restoreTabIndexes();
                    if (_me.Mask != undefined) { _me.Mask.style.display = 'none'; }
                }
                setTimeout(_me.destroy, 1000);
            }
        }
    };
}

function removeChildSafe(el) {
    //before deleting el, recursively delete all of its children.
    while (el.childNodes.length > 0) {
        if (el.childNodes[el.childNodes.length - 1].nodeName == 'IFRAME') {
            el.childNodes[el.childNodes.length - 1].src = "javascript:false";
        }
        removeChildSafe(el.childNodes[el.childNodes.length - 1]);
    }
    el.parentNode.removeChild(el);
    //el.innerHTML="";
}

function getLocationPoint(width, height, loc) {
    var theBody = document.getElementsByTagName("BODY")[0];
    var scTop = parseInt(getScrollTop(), 10);
    var scLeft = parseInt(theBody.scrollLeft, 10);
    var titleBarHeight = 0; // parseInt(_me.MDTB.offsetHeight, 10);
    var fullHeight = getViewPortHeight();
    var fullWidth = getViewPortWidth();
    var x, y;
    switch (loc) {
        case locTypes.NW:
            x = "2px";
            y = "2px";
            break;
        case locTypes.N:
            x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
            y = "2px";
            break;
        case locTypes.NE:
            x = (getViewPortWidth() - width) + "px";
            y = "2px";
            break;
        case locTypes.E:
            x = (getViewPortWidth() - width) + "px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + (10) + "px"
            break;
        case locTypes.SE:
            break;
        case locTypes.S:
            break;
        case locTypes.SW:
            break;
        case locTypes.W:
            x = "2px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
            break;
        case locTypes.CEN:
            x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
            break;
        default: break;
    }
    return { x: x, y: y };
};


function postBackbPrevDlg(eventTarget, eventArg) {
    var el = xGetElementById("mdlDlgFrame_" + (gIteration - 1));
    if (el != null) {
        top.frames["mdlDlgFrame_" + (gIteration - 1)].__doPostBack(eventTarget, eventArg);
    }
}

function refreshPrevDlg(dlgId) {
    if (dlgId != null) {
        var el = _gE(dlgId);
    }
    else {
        var el = xGetElementById("mdlDlgFrame_" + (gIteration - 1));
    }
    
    el.src = el.src;
    return;
}

function getPrevDlg(dlgId) {
    if (dlgId != null) {
        var el = top.frames[dlgId].document.parentWindow;
    }
    else {
        var el = top.frames["mdlDlgFrame_" + (gIteration - 1)].document.parentWindow;
    }
    
    return el;
}
